import SelectOptions from "../../async/select-options.vue";
/**
 * 当是配伍题中的小题时，选项内容为大题选项且不能修改和删除
 */
export default {
    name:'q-choice-edit',
    components:{
        SelectOptions,
    },
    /**
     * @param {Array} options 选项参数
     * @param {Array | String} answer 答案 多选答案及单选
     * @param {String} type 题型  默认参数：''（配伍题使用，没有答案）； S：单选；M | I： 多选；
     */
    props:{
        value: {
            type:Object,
            default:()=>{}
        },
        // 题型
        questionType:{
            type:String,
            default:"S"
        },
        //大题题型
        parentType:{
            type:String,
            default:""
        }
    },
    data(){
        return{
            questionForm:{},
            answers:[], // 多选答案
            answer:"", // 单选答案
            errorText:''//答案未选择或多选少选提示
        };
    },
      watch:{
        value:{
            immediate:true,
            deep:true,
            handler(val) {
                this.questionForm = JSON.parse(JSON.stringify(val));
                if(!val.optionList || !val.optionList.length) {
                    this.$set(this.questionForm,'optionList',[
                        {optionSeq:'A',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()},
                        {optionSeq:'B',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()},
                        {optionSeq:'C',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()},
                        {optionSeq:'D',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()}
                    ]);
                }else if(val.optionList && val.optionList.length){//编辑回显
                    this.questionForm.optionList.forEach((option)=>{
                        option.key = Date.now()+Math.random();
                    });
                    const answerOption = val.optionList.filter(option=>option.isAnswer==='Y');
                    if(answerOption.length){
                        if(this.questionType==='S'){//单选题
                            this.answer = answerOption[0].optionSeq;
                        }else{//多选或不定项选择题
                            this.answers = answerOption.map(option=>option.optionSeq);
                        }
                    }
                }
            }
        },
        'questionForm.optionList':{
            immediate:true,
            deep: true,
            handler(val){
                val && this.setAnswer();
            }
        },
        questionForm:{
            immediate:true,
            deep: true,
            handler(val){
                this.$emit('change',val);
            }
        }
    },
    methods:{
        checkOption(rule, value, callback) {
            if ( value.answerOption === undefined || value.answerOption === ""){
                return callback(new Error("选项不能为空"));
            }
            const isReceipt = this.questionForm.optionList.some(option=>option.answerOption===value.answerOption && option.optionSeq!==value.optionSeq);
            if( isReceipt) {
                return callback(new Error("选项不能重复"));
            }
            callback();
        },
        // 校验答案选择
        api_checkAnswer(){
            const answerOption = this.questionForm.optionList.filter(option=>option.isAnswer==='Y');
            if(this.questionType==='S'  && answerOption.length<1){
                this.errorText = '单选题必须选择一个选项作为正确答案';
            } else if((this.questionType === 'M' ) && answerOption.length<2) {
                this.errorText = '多选题至少选择两个答案选项';
            }else if((this.questionType === 'I' ) && answerOption.length<1) {
                this.errorText = '不定项选择题至少选择一个答案选项';
            }else{
                this.errorText = '';
            }
            return  !this.errorText;
        },
        change(e){
            let answerSeq = 1;
            // 单选
            if(this.questionType==='S'){
                this.questionForm.optionList.forEach(item=>{
                    item.isAnswer =  item.optionSeq === e ? 'Y' : 'N';
                    item.answerSeq = item.isAnswer==='Y'?1:0;//单选题答案序号为1
                });
            }
            // 多选
            if(this.questionType === 'M' || this.questionType === 'I') {
                this.questionForm.optionList.forEach(item => {
                   item.isAnswer = e.indexOf(item.optionSeq) !== -1 ? 'Y' : 'N';
                   if(e.indexOf(item.optionSeq) !== -1) {
                        item.isAnswer==='Y';
                        item.answerSeq = answerSeq;
                        answerSeq++;
                   } else {
                     item.isAnswer==='N';
                     item.answerSeq = 0;
                   }
                });
            }
        },
        setAnswer(){
            const answerOptionList = this.questionForm.optionList.filter( option=>option.isAnswer==='Y');
            const answerList =  answerOptionList.map(option=>option.optionSeq);
            this.questionForm.answer =  answerList.join(',');
            console.log("this.questionForm.answer",this.questionForm.answer);
            //没有答案选项
            if(!answerOptionList.length && this.questionType==='S'){
                this.answer = '';
            }
            if(this.questionType === 'M' || this.questionType === 'I'){
                this.answers = answerList;
            }
        },
        /**
         * 添加选项
         */
        add(){
            let optionList = this.questionForm.optionList;
            if (optionList.length > 25) {
				return this.$message.warning("最多可添加26个选项");
			}
			let len = optionList.length + 65;
            let param = {
				optionSeq: String.fromCharCode(len),
				answerOption:"",
                isAnswer:'N',
                answerSeq:0,
                key: Date.now()
			};
			this.questionForm.optionList.push(param);
        },
        /**
         * 删除选项;最后一项显示删除，只保留两个选项
         */
        del(index){
            this.questionForm.optionList.splice(index,1);
        },
        // 编辑
        handleEdit(value,key,index){
            this.$emit('edit',value,key,index);
        },
        /**
         * 获取选项
         */
        apis_getOptions(){
            return this.options;
        },
        /**
         * 获取题干
         */
        apis_getQuestionTitle(){
            return this.questionTitle;
        },
        // 获取答案
        apis_getAnswers(){
            return this.type === 'S' ? this.answer : this.answers;
        }
    }
};
